<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace common\models;

use backend\models\GoodsCategoryBrGroup;
use Yii;
use yii\db\ActiveRecord;

class GoodsCategoryBrRelation extends ActiveRecord{
	const USE_FLG_YES	= 1;		//启用
	const USE_FLG_NO	= 0;		//禁用

	const BRAND_URL_YES	=1;			//有链接
	const BRAND_URL_NO	=0;			//无连接

    public static function tableName(){
        return '{{%goods_category_br_relation}}';
    }

    //根据id获取分类品牌列表
    public function getCategoryBrandRelationListById($cate_id){
        $where = ['use_flg'=>1,'category_id'=>$cate_id];
        $result = $this::find()->where($where)->asArray()->orderBy('sort asc')->all();

        return $result;
    }

    //获取全部品牌列表，页面导航栏用
    public function getBrandList(){
        $where = [
			self::tableName().'.use_flg'=>self::USE_FLG_YES

		];
        $list = array();
        $result = self::find()
			->where($where)
			->select([
				self::tableName().'.id',
				self::tableName().'.category_id',
				self::tableName().'.group_id',
				self::tableName().'.group_name',
				self::tableName().'.group_alias',
				self::tableName().'.brand_id',
				self::tableName().'.brand_name',
				self::tableName().'.sort'
			])
			//->joinWith('goodsBrand')
            ->joinWith('goodsCategoryBrGroup')
			->joinWith('sysConfig')
			->joinWith('goodsBrand')
			->orderBy([SysConfig::tableName().'.sort'=>SORT_ASC])
			//->createCommand()->getRawSql();
			->asArray()
			->all();

		if(!empty($result)){
            foreach($result as $value){

				//20171222 品牌街链接到指定地方 需求方指定做  获取对应的品牌是否有品牌街
				$value['brand_url'] ='';
				if(isset($value['sysConfig']) && isset($value['sysConfig']['ext1'])){
					$value['brand_url']	= ($value['sysConfig']['config_value'])?$value['sysConfig']['config_value']:'';	//返回 只用于导航为1
				}

                $list[$value['category_id']][$value['group_id']]['brand_id'] = $value['goodsCategoryBrGroup']['id'];
                $list[$value['category_id']][$value['group_id']]['group_name'] = $value['goodsCategoryBrGroup']['group_name'];
                $list[$value['category_id']][$value['group_id']]['brand_list'][] = $value;
            }
        }

        return $list;
    }


	/**
	 * @param 根据分类和对应的品牌获取对应的信息
	 */
	public function getGoodsCategoryBrandRelation($brandId,$cagetryId=''){
		return self::findOne([
			'use_flg'		=>self::USE_FLG_YES,
			'brand_id'		=>$brandId,
			'category_id'	=>$cagetryId
		]);
	}

	/**
	 * @param 关联品牌信息
	 */
	public function getGoodsBrand(){
		return $this->hasOne(GoodsBrand::className(),['id'=>'brand_id'])->where([GoodsBrand::tableName().'.use_flg'=>GoodsBrand::USE_FLG_YES]);
	}

    /**
     * @param 关联品牌组信息
     */
    public function getGoodsCategoryBrGroup(){
        return $this->hasOne(GoodsCategoryBrGroup::className(),['id'=>'group_id'])->where([GoodsCategoryBrGroup::tableName().'.use_flg'=>GoodsCategoryBrGroup::STATUS_ENABLE]);
    }
	/**
	 * @param 关联品牌组信息
	 */
	public function getSysConfig(){
		return $this->hasOne(SysConfig::className(),['ext1'=>'brand_id']);
	}
}